package com.link.server.ws;

import com.link.database.entity.DBAccount;
import com.link.server.ws.handle.Handler;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WebSocketChannel {
    private static  Map<String, ChannelHandlerContext> map=new ConcurrentHashMap<>();
    public synchronized static void add(String id,ChannelHandlerContext session){
        map.put(id,session);
    }
    public synchronized static void remove(String id){
        map.remove(id);
    }
    public synchronized static ChannelHandlerContext get(String id){
        return map.get(id);
    }


    public static DBAccount getByToken(String token) {

      return   map.values().stream()
                .filter(entry -> token.equals(entry.channel().attr(Handler.VERIFICATION_CODE_KEY).get()))
                .findFirst()
                .map(it-> it.channel().attr(Handler.ACCOUNT_KEY).get())
                .orElse(null);
    }
}
